package com.oa.co.service.impl;
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */

import com.oa.co.common.R;
import com.oa.co.mapper.TravelRequestMapper;
import com.oa.co.pojo.Expense_account;
import com.oa.co.pojo.LeaveInformation;
import com.oa.co.pojo.Schedule;
import com.oa.co.pojo.Travel_request;
import com.oa.co.service.ExpenseAccountService;
import com.oa.co.service.ScheduleService;
import com.oa.co.service.TravelRequestService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author sunXuDong
 * @data 2023/5/8
 */
@Service("/travelRequestService")
public class TravelRequestServiceImpl implements TravelRequestService {
    @Autowired
    private TravelRequestMapper travelRequestMapper;
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private ExpenseAccountService expenseAccountService;
    @Override
    @Transactional
    public R allDate(Travel_request travel_request) {
        if (travel_request!=null){
           Travel_request travel_request1 = travelRequestMapper.queryProname(travel_request.getApplicant(),travel_request.getT_id(),travel_request.getProname());
           if (travel_request1==null){
               Integer add = travelRequestMapper.allDate(travel_request);
               Schedule schedule = Schedule.getInstance();
               BeanUtils.copyProperties(travel_request,schedule);
               R add1 = scheduleService.add(schedule);

               if (travel_request.getAir_ticket()==1){
                   Expense_account expense_account = new Expense_account();
                BeanUtils.copyProperties(travel_request,expense_account);
                expense_account.setF_id(21);
                expense_account.setAmount_of_reimbursement(1000.00);
                String d_id = "";
                String[] split = expense_account.getD_id().split("-");
                   d_id="BX-"+split[1];
                   expense_account.setD_id(d_id);
                   R add2 = expenseAccountService.allDate(expense_account);
                   if (add == 1 && add1.getCode() ==200 && add2.getCode()==200){
                       return R.ok();
                   }
                   return R.error().message("系统错误");
               }
               if (add == 1 && add1.getCode() ==200){
                   return R.ok();
               }
               return R.error().message("系统错误");
           }
          return R.error().message("次项目已经申请,请勿重复申请");
        }

        return R.error();
    }

    @Override
    public R deleteDate(Travel_request travel_request) {
        Schedule schedule = scheduleService.queryId(travel_request.getD_id());
        if (schedule.getD_code()==6){
            Integer del = scheduleService.del(travel_request.getD_id());
            Integer del1 = travelRequestMapper.del(travel_request.getD_id());
            if (Objects.equals(del, del1)){
                return  R.ok().message("撤回成功");
            }
            return R.error();
        }else {
            return R.error().message("已在审批,撤回失败");
        }
    }

    @Override
    public R query(String d_id) {
        List<Travel_request> list = travelRequestMapper.query(d_id);

        return list !=null? R.ok().data("res",list):R.error();
    }


}
